<?php
require_once 'PerformanceMetrics.php';
require_once 'PerformanceMetricsOfficers.php';

class Officers extends HTech_Db_Table implements Emms_Interface_Performance, Emms_Interface_Geo
{        
    protected $_name    = 'view_officers';
    protected $_primary = 'OfficerId';

    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Loan officers");
        $this->_gridMetadata['onClick'] = 'admin.officer.view';
        $this->_gridMetadata['params'] = array('LocationId');
        $this->_gridMetadata['headers'] = array
            (
                array('Username',            80, $this->translate("Username"),     'String'),
                array('Role',               100, $this->translate("Role"),         'String'),
                array('SystemAccessToDate', 150, $this->translate("Expiration"),   'DateTime', 'Date'),
                array('LocationShortName',   70, $this->translate("Office"),       'String'),
                array('GovCode',            180, $this->translate("Gov. code"),    'String'),
                array('FullName',           180, $this->translate("Name"),         'String'),
                array('Email',              150, $this->translate("Email"),        'String'),
                array('MobilePhone',         90, $this->translate("Cell phone"),   'String'),
                array('LocationPhone',       90, $this->translate("Office phone"), 'String'),
                array('Status',              90, $this->translate("Status"),       'String'),
                array('Birthdate',          150, $this->translate("Birthdate"),    'DateTime', 'Date')
            );
        return $this;
    }
    
    protected function _setRecordPerformance() 
    {
        $this->_record->p = $this->getPerformance($this->_record->d, new Zend_Date());
    }
            
    public function getPoints($id)
    {  
        $communities = Zend_Registry::get('db')->fetchAll(sprintf('
            select distinct b.Community title, c.Latitude lat, c.Longitude lng, "%s" category
            from view_clients b, communities c 
            where b.OfficerUserId = %s and c.CommunityId = b.CommunityId', self::EMMS_COMMUNITY, $id));            
        $locations = Zend_Registry::get('db')->fetchAll(sprintf('
            select Location title, Latitude lat, Longitude lng, "%s" category
            from view_officers 
            where OfficerUserId = %s', self::EMMS_LOCATION, $id));
        return array_merge($communities, $locations);
    }
        
    public function getPerformanceCharts(Zend_Db_Table_Row $row)
    {   
        return array(
            'title' => $row['FullName'],
            'subtitle' => self::translate('Performance charts'),
            'params' => array(
                'p1' => 'OfficerUserId',
                'v1' => $row['OfficerUserId'],
                'p2' => 'Username',
                'v2' => $row['Username']),
            'charts' => array(
                'day' => array(
                    'chartId' => 'OfficerPerformanceDay',
                    'chartData' => '/admin/chart/performance/Mode/Day/Subject/Officer/id/' . $row['OfficerUserId'],
                    'chartSizeX' => 350,
                    'chartSizeY' => 320),
                'week' => array(
                    'chartId' => 'OfficerPerformanceWeek',
                    'chartData' => '/admin/chart/performance/Mode/Week/Subject/Officer/id/' . $row['OfficerUserId'],
                    'chartSizeX' => 200,
                    'chartSizeY' => 320),
                'month' => array(
                    'chartId' => 'OfficerPerformanceMonth',
                    'chartData' => '/admin/chart/performance/Mode/Month/Subject/Officer/id/' . $row['OfficerUserId'],
                    'chartSizeX' => 600,
                    'chartSizeY' => 320)
            )
        );
    }
    
    public function getPerformance(Zend_Db_Table_Row $row, Zend_Date $date, $options = array())
    {
        $row = $row; $date = $date; $options = $options;
        
        $ps = array();
        $pc = array();
        
        $pf = Zend_Registry::get('db')->query("select * from performance_data where Date = curdate() and OfficerUserId = ?", 
                array($row['OfficerUserId']))->fetchAll();
        
        foreach($pf as $metric) {
            $ps[$metric->Metric] = $metric->Value;
            $pc[$metric->Metric] = $metric->Color;
        }
        $ps['c'] = (object) $pc;
        return (object) $ps;
    }
       
    static function getOfficerOptions($type = '', $id = 0)
    {
        if(!$id)            
            return array('' => '--- ' . self::translate('Select loan officer')) + 
                Zend_Registry::get('db')->fetchPairs("select OfficerUserId, concat(LocationShortName,': ',FullName) FullName 
                    from view_officers order by LocationShortName, FullName");
        switch($type) {
            case 'Location':
                return Zend_Registry::get('db')->fetchPairs("select OfficerUserId, concat(LocationShortName,': ',FullName) FullName 
                    from view_officers where LocationId = ? order by LocationShortName, FullName", $id);
            case 'Officer':
                return Zend_Registry::get('db')->fetchPairs("select o.OfficerUserId, concat(o.LocationShortName,': ',o.FullName) FullName 
                    from view_officers o, view_officers o2 where o.LocationId = o2.LocationId and o2.officerUserId = ? order by o.LocationShortName, o.FullName", $id);
            case 'Bank':
                require_once 'Banks.php';
                if(Banks::getActiveLoansCount($id)) {
                    return Zend_Registry::get('db')->fetchPairs("select o.OfficerUserId, concat(o.LocationShortName,': ',o.FullName) FullName 
                        from view_officers o, view_banks b where o.OfficerUserId = b.OfficerUserId and b.BankId = ?", $id);
                } else {
                    return array('' => '--- ' . self::translate('Select loan officer')) + 
                        Zend_Registry::get('db')->fetchPairs("select OfficerUserId, concat(LocationShortName,': ',FullName) FullName 
                            from view_officers order by LocationShortName, FullName");
                }
            case 'Client':
                require_once 'Clients.php';
                if(Clients::getActiveLoansCount($id)) {
                    return Zend_Registry::get('db')->fetchPairs("select o.OfficerUserId, concat(o.LocationShortName,': ',o.FullName) FullName 
                        from view_officers o, view_clients c where o.OfficerUserId = c.OfficerUserId and c.ClientId = ?", $id);
                } else {            
                    return array('' => '--- ' . self::translate('Select loan officer')) + 
                        Zend_Registry::get('db')->fetchPairs("select OfficerUserId, concat(LocationShortName,': ',FullName) FullName 
                            from view_officers order by LocationShortName, FullName");
                }
        }
        return array();
    } 
}
